package com.supplychainapi.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.joindata.inf.common.basic.exceptions.ServiceException;
import com.supplychainapi.common.enums.ExceptionMessage;
import com.supplychainapi.dao.prod.ScmTbstorageMapper;
import com.supplychainapi.dao.prod.TbstaffMapper;
import com.supplychainapi.model.ScmTbseqno;
import com.supplychainapi.model.Tbstaff;
import com.supplychainapi.model.TbstaffExtend;
import com.supplychainapi.param.ColTitle;
import com.supplychainapi.service.ITbstaffService;
import com.supplychainapi.service.TbSeqNoService;
import com.supplychainapi.utils.Constants;
import com.supplychainapi.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 
 * 员工列表
 * @author:xiezhiwei
 * @email:xiezhiwei@niwodai.net
 * @date:2017-10-25
 *
 */
@Service
public class TbstaffServiceImpl implements ITbstaffService {
	
	private Logger logger = LoggerFactory.getLogger(TbstaffServiceImpl.class);
	
	@Autowired
	private TbstaffMapper tbstaffMapper;
	

	@Autowired
	private ScmTbstorageMapper storageMapper;
	
    @Autowired
    TbSeqNoService tbSeqNoService;
	

	@Override
	public void addEmployeeInfo(Tbstaff staff) throws ServiceException {
		
		if(StringUtil.checkIsEmoji(staff.getFsStaffName())){
			throw ExceptionMessage.emoji_failed.getServiceException();
		}
		if(staff.getFsStaffId()==null || staff.getFsStaffId().trim().equals("")){
			logger.info("----开始自动生成员工编号-----");
			ScmTbseqno model = new ScmTbseqno();
			model.setFsShopGUID(staff.getFsShopGUID());
			model.setFsCls(Constants.TBSTAFF);
			String staffId = tbSeqNoService.selectSeqNo(model);
			staff.setFsStaffId(staffId);
		}else{
			logger.info("手动填写编号业务处理");
            ScmTbseqno tbseqno1=new ScmTbseqno();
            tbseqno1.setFiSeqNo(staff.getFsStaffId());
            tbseqno1.setFsCls(Constants.TBSTAFF);
            tbseqno1.setFsShopGUID(staff.getFsShopGUID());
            tbSeqNoService.checkCode(tbseqno1);
            tbSeqNoService.insert(tbseqno1,staff.getFsShopGUID());
			
		}
		
		staff.setFsCreateTime(new Date());
		staff.setFsUpdateTime(new Date());
		tbstaffMapper.insertSelective(staff);
	}


	@Override
	public void updateEmployeeInfo(Tbstaff staff) throws ServiceException {
		staff.setFsCreateTime(new Date());
		staff.setFsUpdateTime(new Date());
		tbstaffMapper.updateByPrimaryKey(staff);
	}

	@Override
	public void deleteEmployeeInfo(String fsShopGUID, String fsStaffId) throws ServiceException{
		logger.info("删除员工信息 fsShopGUID={},fsStaffId={}",fsShopGUID,fsStaffId);
		Integer count = storageMapper.getStaffIdInfo(fsStaffId);
		if(count!=null && count>0){
			 logger.info("当前员工已绑定仓库");
			 throw ExceptionMessage.operation_failed.getServiceException();
		}
		Tbstaff staff = new Tbstaff();
		staff.setFsShopGUID(fsShopGUID);
		staff.setFsStaffId(fsStaffId);
		tbstaffMapper.deleteByPrimaryKey(staff);
	}
	
	@Override
	public PageInfo getStaffListInfo(String fsShopGUID,String keyWord,Integer pageNum,Integer pageSize) throws ServiceException {
		logger.info("fsShopGUID={},keyWord={},page={},pageSize={}",fsShopGUID,keyWord,pageNum,pageSize);
	    PageHelper.startPage(pageNum, pageSize);
	    PageHelper.orderBy("fsCreateTime DESC");
		List<TbstaffExtend> staffList = tbstaffMapper.getStaffListInfo(fsShopGUID,keyWord);
		return new PageInfo(staffList);
	}


	@Override
	public int count(Tbstaff param) {
		return tbstaffMapper.selectCount(param);
	}


	@Override
	public List<ColTitle> getSchema() {
		List<ColTitle> colTitles = Lists.newArrayList();
		colTitles.add(new ColTitle("fsStaffId", "员工编号", "center", 100));
		colTitles.add(new ColTitle("fsStaffName", "员工名称", "center", 100));
		colTitles.add(new ColTitle("fsCellphone", "手机号", "center", 100));
		colTitles.add(new ColTitle("fsDepartmentName", "部门", "center", 100));
		colTitles.add(new ColTitle("fsNote", "备注", "center", 100));
		colTitles.add(new ColTitle("fiStatus", "状态", "center", 100));
        colTitles.add(new ColTitle("__EDIT__", "操作", null, 150, "fsStaffId"));
        return colTitles;
    }


	@Override
	public List<ColTitle> getChoiceSchema() {
		List<ColTitle> colTitles = Lists.newArrayList();
		colTitles.add(new ColTitle("fsStaffId", "员工编号", "center", 100));
		colTitles.add(new ColTitle("fsStaffName", "员工名称", "center", 100));
		colTitles.add(new ColTitle("fsCellphone", "手机号", "center", 100));
		colTitles.add(new ColTitle("fsDepartmentName", "部门", "center", 100));
		colTitles.add(new ColTitle("fsNote", "备注", "center", 100));
		return colTitles;
	}


	@Override
	public PageInfo getChoiceStaffListInfo(String fsShopGUID, String keyWord, Integer pageNum, Integer pageSize) throws ServiceException{
	    PageHelper.startPage(pageNum, pageSize);
	    PageHelper.orderBy("fsCreateTime DESC");
		List<TbstaffExtend> staffList = tbstaffMapper.getChoiceStaffListInfo(fsShopGUID,keyWord);
		return new PageInfo(staffList);
	}


	@Override
	public TbstaffExtend getStaffDetailsInfo(String fsShopGUID, String fsStaffId) throws ServiceException {
		return tbstaffMapper.getStaffDetailsInfo(fsShopGUID,fsStaffId);
	}



}
